﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bzure.Common.Data;
using Bzure.Common.Configuration.AppConfiguration;

namespace Bzure.Apps.Security.DataAccess
{
    public class ConfigTemplateRepository : RepositoryBase
    {
        static ConfigTemplateRepository()
        {
            ConfigTemplate entity = new ConfigTemplate();
            Type type = entity.GetType();
            AppendDataMap(type, "TKey", () => entity.TemplateKey);
            AppendDataMap(type, "TExpression", () => entity.Expression);
            AppendDataMap(type, "TValue", () => entity.Value);
            ResultMapDelegate<ConfigTemplate> queryMap = ConfigTemplate_Map;
            QueryMapContainer[entity.GetType()] = queryMap;



        }
        public ConfigTemplateRepository(Bzure.Common.Transactions.ITransactionSupport contractProvider)
            : base(contractProvider)
        {
        }
        public override string TableName
        {
            get
            {
                return "T_Security_Settings";
            }
        }
        /// <summary>
        /// 收藏实体数据映射
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static ConfigTemplate ConfigTemplate_Map(DataReader reader)
        {
            return new ConfigTemplate
            {
                TemplateKey = reader.GetStringNullable("TKey"),
                Expression = reader.GetStringNullable("TExpression"),
                Value = reader.GetStringNullable("TValue")

            };
        }
        public ConfigTemplate GetConfigTemplate(string TemplateKey)
        {
            string sql = string.Format("SELECT * FROM  {0} WHERE TKey = @TKey", this.TableName);
            return new SqlEntityMap<ConfigTemplate>(this.ContractProvider, sql)
                .AddString("TKey", TemplateKey)
                .SetMap(ConfigTemplate_Map)
                .AsSingle();
        }
    }
}
